package cn.vrview.webtoandroid;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 作者：Administrator on 2017/5/24 16:50
 * 描述：
 */

public class DetailActivity extends AppCompatActivity {

    private WebView webView;
    private TextView android_text;
    private Button button;
    private EditText edit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        webView = (WebView) findViewById(R.id.webView);
        button = (Button) findViewById(R.id.button);
        android_text = (TextView) findViewById(R.id.android_text);
        edit = (EditText) findViewById(R.id.edit);

        initData();

        //在Android中点击按钮，传递给HTML
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.post(new Runnable() {
                    @Override
                    public void run() {
                        String str = edit.getText().toString();
                        webView.loadUrl("javascript:AndroidToWeb('" + str + "')");
                        webView.loadUrl("javascript:showConfirm('" + str + "')");
                        Log.e("Logs", "-----------------------wave--------------------");
                    }
                });
            }
        });

    }

    @SuppressLint("JavascriptInterface")
    private void initData() {

        //设置支持js
        webView.getSettings().setJavaScriptEnabled(true);
        webView.requestFocus();
        webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        //设置这个是为了保证在JavaScript中alert的对话框时能显示
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }
        });
        //在HTML中输入数据，点击按钮，将字符串返回给Android
        //Webview提供的传入js的方法
        //demo解释：生成对象的别名，以便在JS中使用
        webView.addJavascriptInterface(
                new JavaJavascriptInterface(), "demo");
        //设置加载网页
        webView.loadUrl("file:///android_asset/myhtml.html");

        //给webview设置加载过程，可以在每一个时期处理一些数据
        WebViewClient webviewc = new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                webView.loadUrl("javascript:showAlert('"+"加载完成了"+"')");
                Log.i("11111", "webview");

            }
        };

        webView.setWebViewClient(webviewc);
    }

    class JavaJavascriptInterface {

        @android.webkit.JavascriptInterface
        public void toAndroid(final String order) {
            Log.e("Logs", "-----------------------" + order);
            webView.post(new Runnable() {
                @Override
                public void run() {
                    android_text.setText(order);
                    android_text.setTextColor(Color.RED);
                }
            });
        }
    }
}
